{{- $name := .Chart.Name }}
{{- $globalAnnotationsCheck := include "global.annotations.check" . }}
{{- $globalContEnvsCheck := include "global.containers.envs.check" . }}
{{- $globalContExtraCheck := include "global.containers.extraEnvs.check" . }}
{{- $globalContRegistry := include "global.containers.registry" . }}
{{- $globalInitEnvsCheck := include "global.initContainers.envs.check" . }}
{{- $globalInitExtraCheck := include "global.initContainers.extraEnvs.check" . }}
{{- $globalInitRegistry := include "global.initContainers.registry" . }}
{{- range $index, $val := .Values.items }}
{{- if or (eq $val.type "cronjob") $val.schedule }}
{{- if or $val.type $val.name | required "for cronJobs need .type or .name !" }}
apiVersion: batch/v1beta1
kind: CronJob
metadata:
{{- if or $val.annotations $globalAnnotationsCheck }}
  annotations:
{{- if $val.annotations }}
{{- toYaml $val.annotations | nindent 4 }}
{{- end }}
{{- if $globalAnnotationsCheck }}
{{- range $Index, $Val := $.Values.items }}
{{- if eq $Val.type "global" }}
{{- toYaml $Val.annotations | nindent 4 }}
{{- end }}{{- end }}{{- end }}
{{- end }}
  name: {{ print $val.name | default (print $name "-" $val.type "-" $index) }}
  labels:
    app: {{ $name }}
    tier: {{ $val.name | default (print $name "-" $val.type) }}
spec:
  schedule: {{ $val.schedule }}
  concurrencyPolicy: {{ $val.concurrencyPolicy | default (print "Forbid") }}
  {{- if $val.successfulJobsHistoryLimit }}
  successfulJobsHistoryLimit: {{ $val.successfulJobsHistoryLimit }}
  {{- end }}
  {{- if $val.failedJobsHistoryLimit }}
  failedJobsHistoryLimit: {{ $val.failedJobsHistoryLimit }}
  {{- end }}
  jobTemplate:
    metadata:
      labels:
        app: {{ $name }}
        tier: {{ $val.name | default (print $name "-" $val.type) }}
    spec:
      {{- if $val.backoffLimit }}
      backoffLimit: {{ $val.backoffLimit }}
      {{- end }}
      {{- if $val.activeDeadlineSeconds }}
      activeDeadlineSeconds: {{ $val.activeDeadlineSeconds }}
      {{- end }}
      template:
        spec:
          {{- if $val.volumes }}
          volumes:
            {{- toYaml .volumes | indent 12 }}
          {{- end }}
          restartPolicy: {{ $val.restartPolicy | default (print "OnFailure") }}
          {{- if $val.imagePullSecrets.enabled }}
          imagePullSecrets:
            - name: {{ $val.imagePullSecrets.name | default (print $name "-dockerconfig") }}
          {{- end }}
          {{- if $val.initContainers }}
          initContainers:
            {{- range $indexInitContainers, $valInitContainers := $val.initContainers }}
            - name: {{ $valInitContainers.name | default (print $name "-init-" $indexInitContainers) }}
              {{- if not $globalInitRegistry }}
              image: {{ print (default "registry.hub.docker.com" $valInitContainers.registry) "/"
              $valInitContainers.image ":" (default "latest" $valInitContainers.imageTag ) }}
              {{- else }}
              image: {{ print $globalInitRegistry "/"
              $valInitContainers.image ":" (default "latest" $valInitContainers.imageTag ) }}
              {{- end }}
              {{- with $valInitContainers.command }}
              command:
                {{- toYaml . | nindent 16 }}
              {{- end }}
              {{- with $valInitContainers.args }}
              args:
                {{- toYaml . | nindent 16 }}
              {{- end }}
              {{- with $valInitContainers.volumeMounts }}
              volumeMounts:
                {{- toYaml . | nindent 16 }}
              {{- end }}
              {{- with $valInitContainers.resources }}
              resources:
                {{- toYaml . | nindent 16 }}
              {{- end }}
              {{- with $valInitContainers.ports }}
              ports:
                {{- toYaml . | nindent 16 }}
              {{- end }}
              {{- if $valInitContainers.imagePullPolicy }}
              imagePullPolicy: {{ $valInitContainers.imagePullPolicy }}
              {{- end }}
              {{- if not (or $valInitContainers.envs $globalInitEnvsCheck) }}
              {{- if or $valInitContainers.configMap $valInitContainers.secret $valInitContainers.extraEnvs $globalInitExtraCheck }}
              {{- if or $valInitContainers.configMap $valInitContainers.secret }}
              {{- if or $valInitContainers.configMap.enabled $valInitContainers.secret.enabled }}
              envFrom:
                {{- if $valInitContainers.configMap.enabled }}
                - configMapRef:
                    name: {{ $valInitContainers.configMap.name | default (print $name "-config") }}
                {{- end }}
                {{- if $valInitContainers.secret.enabled }}
                - secretRef:
                    name: {{ $valInitContainers.secret.name | default (print $name "-secret") }}
                {{- end }}
              {{- end }}{{- end }}
              {{- if or $valInitContainers.extraEnvs $globalInitExtraCheck }}
              env:
                {{- if $valInitContainers.extraEnvs }}
                {{- range $valInitContainers.extraEnvs }}{{- if .value }}
                - name: {{ .name }}
                  value: {{ .value | quote }}
                {{- end }}{{- end }}{{- end }}
                {{- range $Ind, $Val := $.Values.items }}
                {{- if eq $Val.type "global" }}
                {{- range $_, $containers := $Val.containers }}
                {{- range $__, $containersExtraEnvs := $containers.extraEnvs }}{{- if $containersExtraEnvs.value }}
                - name: {{ $containersExtraEnvs.name }}
                  value: {{ $containersExtraEnvs.value | quote }}
                {{- end }}{{- end }}{{- end }}
                {{- end }}{{- end }}
              {{- end }}
              {{- end }}
              {{- else }}
              env:
                {{- if and (not $globalInitEnvsCheck) $valInitContainers.envs }}
                {{- range $valInitContainers.envs }}{{- if .value }}
                - name: {{ .name }}
                  value: {{ .value | quote }}
                {{- end }}{{- end }}{{- end }}
                {{- if $globalInitEnvsCheck }}
                {{- range $Ind, $Val := $.Values.items }}
                {{- if eq $Val.type "global" }}
                {{- range $_, $containers := $Val.containers }}
                {{- range $__, $containersEnvs := $containers.envs }}{{- if $containersEnvs.value }}
                - name: {{ $containersEnvs.name }}
                  value: {{ $containersEnvs.value | quote }}
                {{- end }}{{- end }}{{- end }}
                {{- end }}{{- end }}
                {{- end }}
              {{- end }}
            {{- end }}
          {{- end }}
          {{- if $val.containers }}
          containers:
            {{- range $indexContainers, $valContainers := $val.containers }}
            - name: {{ $valContainers.name | default (print $name "-cont-" $indexContainers) }}
              {{- if not $globalContRegistry }}
              image: {{ print (default "registry.hub.docker.com" $valContainers.registry) "/"
              $valContainers.image ":" (default "latest" $valContainers.imageTag ) }}
              {{- else }}
              image: {{ print $globalContRegistry "/"
              $valContainers.image ":" (default "latest" $valContainers.imageTag ) }}
              {{- end }}
              {{- with $valContainers.command }}
              command:
                {{- toYaml . | nindent 16 }}
              {{- end }}
              {{- with $valContainers.args }}
              args:
                {{- toYaml . | nindent 16 }}
              {{- end }}
              {{- with $valContainers.volumeMounts }}
              volumeMounts:
                {{- toYaml . | nindent 16 }}
              {{- end }}
              {{- with $valContainers.livenessProbe }}
              livenessProbe:
                {{- toYaml . | nindent 16 }}
              {{- end }}
              {{- with $valContainers.readinessProbe }}
              readinessProbe:
                {{- toYaml . | nindent 16 }}
              {{- end }}
              {{- with $valContainers.resources }}
              resources:
                {{- toYaml . | nindent 16 }}
              {{- end }}
              {{- with $valContainers.ports }}
              ports:
                {{- toYaml . | nindent 16 }}
              {{- end }}
              {{- if $valContainers.imagePullPolicy }}
              imagePullPolicy: {{ $valContainers.imagePullPolicy }}
              {{- end }}
              {{- if not (or $valContainers.envs $globalContEnvsCheck) }}
              {{- if or $valContainers.configMap $valContainers.secret $valContainers.extraEnvs $globalContExtraCheck }}
              {{- if or $valContainers.configMap $valContainers.secret }}
              {{- if or $valContainers.configMap.enabled $valContainers.secret.enabled }}
              envFrom:
                {{- if $valContainers.configMap.enabled }}
                - configMapRef:
                    name: {{ $valContainers.configMap.name | default (print $name "-config") }}
                {{- end }}
                {{- if $valContainers.secret.enabled }}
                - secretRef:
                    name: {{ $valContainers.secret.name | default (print $name "-secret") }}
                {{- end }}
              {{- end }}{{- end }}
              {{- if or $valContainers.extraEnvs $globalContExtraCheck }}
              env:
                {{- if $valContainers.extraEnvs }}
                {{- range $valContainers.extraEnvs }}{{- if .value }}
                - name: {{ .name }}
                  value: {{ .value | quote }}
                {{- end }}{{- end }}{{- end }}
                {{- range $Ind, $Val := $.Values.items }}
                {{- if eq $Val.type "global" }}
                {{- range $_, $containers := $Val.containers }}
                {{- range $__, $containersExtraEnvs := $containers.extraEnvs }}{{- if $containersExtraEnvs.value }}
                - name: {{ $containersExtraEnvs.name }}
                  value: {{ $containersExtraEnvs.value | quote }}
                {{- end }}{{- end }}{{- end }}
                {{- end }}{{- end }}
              {{- end }}
              {{- end }}
              {{- else }}
              env:
                {{- if and (not $globalContEnvsCheck) $valContainers.envs }}
                {{- range $valContainers.envs }}{{- if .value }}
                - name: {{ .name }}
                  value: {{ .value | quote }}
                {{- end }}{{- end }}{{- end }}
                {{- if $globalContEnvsCheck }}
                {{- range $Ind, $Val := $.Values.items }}
                {{- if eq $Val.type "global" }}
                {{- range $_, $containers := $Val.containers }}
                {{- range $__, $containersEnvs := $containers.envs }}{{- if $containersEnvs.value }}
                - name: {{ $containersEnvs.name }}
                  value: {{ $containersEnvs.value | quote }}
                {{- end }}{{- end }}{{- end }}
                {{- end }}{{- end }}
                {{- end }}
              {{- end }}
            {{- end }}
          {{- end }}
{{ print "---" }}
{{- end }}{{- end }}{{- end }}
